View Javadoc

1   /*
2    * Copyright (C) 1998-2000 Semiotek Inc.  All Rights Reserved.
3    *
4    * Redistribution and use in source and binary forms, with or without
5    * modification, are permitted under the terms of either of the following
6    * Open Source licenses:
7    *
8    * The GNU General Public License, version 2, or any later version, as
9    * published by the Free Software Foundation
10   * (http://www.fsf.org/copyleft/gpl.html);
11   *
12   *  or
13   *
14   * The Semiotek Public License (http://webmacro.org/LICENSE.)
15   *
16   * This software is provided "as is", with NO WARRANTY, not even the
17   * implied warranties of fitness to purpose, or merchantability. You
18   * assume all risks and liabilities associated with its use.
19   *
20   * See www.webmacro.org for more information on the WebMacro project.
21   */
22  package org.webmacro.util;
23  
24  import org.webmacro.Broker;
25  import org.webmacro.WebMacroException;
26  
27  import java.lang.reflect.Constructor;
28  
29  /***
30   * The LogTargetFactory assists the Broker (and you, if you want) in creating
31   * new LogTarget instances.<p>
32   *
33   * If your LogTarget needs configuration settings from WebMacro, create a
34   * constructor with this signature:<pre>
35   *
36   *     public MyLogTarget (org.webmacro.util.Settings settings);
37   *
38   * </pre>
39   *
40   * If you don't need to configuration options, you should have a
41   * null constructor.
42   *
43   * @author  e_ridge
44   * @since 0.99
45   */
46  public class LogTargetFactory
47  {
48  
49      private static LogTargetFactory _instance = new LogTargetFactory();
50  
51      public static class LogCreationException extends WebMacroException
52      {
53  
54          /***
55  		 * 
56  		 */
57  		private static final long serialVersionUID = 1L;
58  
59  		public LogCreationException (String message, Throwable throwable)
60          {
61              super(message, throwable);
62          }
63      }
64  
65      /*** Creates new LogTargetFactory */
66      private LogTargetFactory ()
67      {
68      }
69  
70      /*** return the only instance of this LogTargetFactory */
71      public static final LogTargetFactory getInstance ()
72      {
73          return _instance;
74      }
75  
76      /***
77       * Creates a new <code>org.webmacro.util.LogTarget</code>
78       *
79       * @param broker the Broker that is requesting to create the log.  The
80       *        Broker is used to find the LogTarget class via the Broker's
81       *        <code>.classForName()</code> method.
82       * @param classname the fully-qualified classname of the LogTarget to create
83       * @param settings WebMacro settings that will be passed off to the
84       *        new LogTarget during its construction
85       */
86      public final LogTarget createLogTarget (Broker broker, String classname, Settings settings) throws LogCreationException
87      {
88          LogTarget lt = null;
89          try
90          {
91              Class targetClass = broker.classForName(classname);
92              Class[] args = new Class[]{Settings.class};
93              try
94              {
95                  // attempt to use the constructor that takes a Settings object
96                  Constructor constr = targetClass.getConstructor(args);
97                  lt = (LogTarget) constr.newInstance(new Object[]{settings});
98              }
99              catch (NoSuchMethodException nsme)
100             {
101                 // otherwise, use the default constructor
102                 lt = (LogTarget) targetClass.newInstance();
103             }
104         }
105         catch (Exception e)
106         {
107             throw new LogCreationException("Cannot create LogTarget "
108                     + classname, e);
109         }
110 
111         return lt;
112     }
113 }